package cn.tedu.blog.server.service.impl;


import cn.tedu.blog.server.exception.CoolSharkServiceException;
import cn.tedu.blog.server.exception.ServiceException;
import cn.tedu.blog.server.mapper.CommentMapper;
import cn.tedu.blog.server.pojo.dto.CommentAddNewDto;
import cn.tedu.blog.server.pojo.entity.Comment;
import cn.tedu.blog.server.pojo.vo.CommentDetailsVo;
import cn.tedu.blog.server.pojo.vo.CommentListVo;
import cn.tedu.blog.server.service.ICommentService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.ResponseCode;
import cn.tedu.blog.server.utils.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    CommentMapper commentMapper;


    @Override
    public void deleteById(Long id) {
        log.info("开始执行删除评论业务  :{}", id);
//        //根据id查询数据

        CommentDetailsVo commentDetailsVo = commentMapper.getById(id);
        //判断查询结果是否为null
        if (commentDetailsVo == null) {
            String message = "删除评论失败,尝试删除的id不存在";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);

        }

        int deleteById = commentMapper.deleteById(id);
        if (deleteById != 1) {
            String message = "删除评论失败,服务器忙请稍后再试";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
        }

    }


    @Override
    public void deleteImpl(Long[] ids) {

        for (int i = 0; i <= ids.length; i++) {
            Long id;
            id = ids[i];
            int count = commentMapper.countById(id);
            if (count != 1) {
                String message = "删除评论失败" + id + "不存在或已删除，请重新刷新列表再试";
                log.info(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
            commentMapper.deleteByIds(ids);
            return;
        }
    }

    @Override
    public void updateStatus(Long id) {
        log.info("");
        int i = commentMapper.updateStatus(id);


    }


    @Override
    public List<CommentListVo> getList() {
        log.debug("开始查询评论列表");
        return commentMapper.list();
        //直接返回Redis中缓存的数据
//        return icommentRepository.getList();

//        // 1. 尝试从Redis中获取数据
//        List<CommentListVo> com = icommentRepository.getList();
//        // 2. 如果Redis中没有有效的数据（集合为空），则尝试从MySQL中获取数据
//        if (com.size() == 0) {
//            List<CommentListVo> commentListVos = commentMapper.list();
//            // 3. 如果从MySQL中获取到数据，则将数据写入到Redis
//            if (commentListVos.size() > 0) {
//                icommentRepository.putList(commentListVos);
//                return commentListVos;
//            }
//            // 4. 如果从MySQL中没有获取到数据（集合为空），则放弃向Redis中写入数据
//        }
//        // 5. 返回Redis的查询结果 / 从MySQL中查询到的有效的、且写入到Redis中的数据
//        return com;


    }

    /* ---------------前台部分 ---------------  */
    // 分页查询评论
    @Override
    public JsonPage<CommentListVo> listByPage(Long articleId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CommentListVo> list = commentMapper.listCommentsByArticleId(articleId);
        return JsonPage.restPage(new PageInfo<>(list));
    }

    // 新增评论
    @Override
    public void saveComment(CommentAddNewDto commentAddNewDto) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewDto, comment);
        // 补全属性
        String nick = commentMapper.getNickByUserId(commentAddNewDto.getUserId());
        comment.setGmtCreate(LocalDateTime.now());
        comment.setNick(nick);
        System.out.println("!!!!!!!~~~~赋值完毕的对~象~~~~:" + comment);
        // 执行新增
        int rows = commentMapper.saveComment(comment);
        if (rows != 1) {
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "服务器忙");
        }
    }

    // 根据id删除评论
    @Override
    public void deleteCommentById(Long id) {
        // 先查出此id的评论是否存在
        CommentDetailsVo commentDetailsVo = commentMapper.getById(id);
        if (commentDetailsVo == null) {
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, "此评论不存在");
        }
        // 代码走到此处说明评论存在, 执行删除
        int rows = commentMapper.deleteComment(id);
        if (rows != 1) {
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "服务器忙");
        }
    }

}
